package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem;

import android.graphics.Bitmap;
import android.widget.ImageView;

import androidx.test.espresso.Espresso;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.google.zxing.WriterException;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.UUID;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.utils.QRCodeUtils;

@RunWith(AndroidJUnit4.class)
public class QRCodeTest {
    @Rule
    public ActivityScenarioRule<MainActivity> activityRule = new ActivityScenarioRule<>(MainActivity.class);

    @Test
    public void testQRCodeGenerateAndDisplay() throws WriterException {
        String productId = UUID.randomUUID().toString();
        Bitmap qrCodeBitmap = QRCodeUtils.generateQRCode(productId, 300, 300);

        Assert.assertNotNull(qrCodeBitmap);

        activityRule.getScenario().onActivity(activity -> {
            ImageView imageView = activity.findViewById(R.id.qrcodeImageView);
            imageView.setImageBitmap(qrCodeBitmap);

            Assert.assertNotNull(imageView.getDrawable());
        });

        Espresso.onView(ViewMatchers.withId(R.id.qrcodeImageView)).check((view, noViewFoundException) -> {
            Assert.assertNotNull(view);
            Assert.assertNull(noViewFoundException);
        });

        try {
            Thread.sleep(50000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
